<template>
	<view class="pay">
		<view class="pay-view">
			<image v-if="payType == 1" src="/static/image/index/paysuccess.png" mode="widthFix"></image>
			<image v-else :src="Url + 'add_images/pay-lose.png'" mode="widthFix"></image>
			<view class="" v-if="payType == 1 ">
				<text>支付成功</text>
				<view class="" v-if="type != 2">
					<p v-if='price'>支付金额：￥{{price}}</p>
					<p style="margin-top: 10rpx;">支付方式：快捷支付</p>
					<p v-if="order_no" style="margin-top: 10rpx;">订单编号：{{order_no}}</p>
				</view>
			</view>
		 
		</view>
		<view class="pay-btn" v-if="type == 3">
			<view @tap="backIndex">返回首页</view>
			<view class="pay-btn-succ" v-if="payType == 1" @tap="taplive">查看订单</view>
			<!-- <view class="pay-btn-lose" v-else @tap="topay">继续支付</view> -->
		</view>
	</view>
</template>

<script>
	import {
		payDo,getSuccessData
	} from '@/common/index.js';
	import {
		debounce
	} from '@/static/js/utils.js';
	export default {
		data() {
			return {
				Url: this.Imgurl,
				payType: 1,
				params: {},
				loading: false,
				opsData: {},
				type: 1,//2只显示图片	
				price: '',
				order_no: '',
				id:''
			};
		},
		onLoad(ops) {
			// this.payType = ops.payType;
			// this.price = ops.price;
			// this.order_no = ops.order_no;
			this.type = ops.type;
			this.id = ops.id;
			// if (ops.payType == 2) {
			// 	this.params = JSON.parse(ops.data);
			// 	this.opsData = JSON.parse(ops.canData);
			// } else {
			// 	this.type = +ops.type
			// }
			// uni.setNavigationBarTitle({
			// 	title: ops.payType == 1 ? '支付成功' : '支付失败'
			// });
			this.getSuccess()
		},
		methods: {
			//获取支付成功信息
			getSuccess(){
				let param = {
					type:this.type,
					id:this.id
				}
				getSuccessData(param).then((res)=>{
					this.price = res.msg.price;
					this.order_no = res.msg.order_no;
					this.payType = res.msg.pay_type;
					this.type = res.msg.type;
		 
				}).catch(()=>{
					this.toast("获取数据失败")
				})
			},
			backIndex() {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			},
			taplive() {
				const {
					params,
					type
				} = this;
				// console.log(params)
				console.log(type)
				let status = +type
				if (status == 1 || status == 0) {
					// console.log('跳转')
					uni.reLaunch({
						url: '/pageD/order/order'
					});
				} else if (status == 3) {
					uni.reLaunch({
						url: '/pageD/order/group_order'
					});
				} else if (status == 2) {
					uni.reLaunch({
						url: '/pageA/integral/changehistory'
					});
				}
			},
			//微信调起支付
			wePay() {
				payDo(this.params)
					.then(res => {
						if (res.code == 1) {
							// console.log(res)
							if (!uni.getSystemInfoSync().version) {
								this.h5pay(res.msg);
							} else {
								this.wechatPay(res.msg);
							}
						}
					})
					.catch();
			},
			//支付
			topay: debounce(function() {
				const {
					params,
					opsData
				} = this;
				var lo = JSON.stringify(opsData);
				const versionFlag = uni.getSystemInfoSync().version;
				if (params.pay_type != 1) {
					uni.showModal({
						title: '支付',
						content: '确定支付吗?',
						success: res => {
							if (res.confirm) {
								payDo(params)
									.then(res => {
										if (res.code == 1) {
											this.toast(res.msg);
											this.loading = false;
											this.payType = 1;
											if (params.type != 3) {
												uni.setNavigationBarTitle({
													title: '支付成功'
												});
											} else {
												if (!versionFlag) {
													window.location.href =
														'https://shop.jyecloud.cn/h5/pageB/groupTuxedo/groupTuxedo?id=' +
														opsData.user_assemble_id + '&type=' +
														opsData.type;
												} else {
													uni.reLaunch({
														url: '/pageB/groupTuxedo/groupTuxedo?id=' +
															opsData.user_assemble_id +
															'&type=' + opsData.type
													});
												}
												// uni.reLaunch({
												// 	url: '/pageB/groupTuxedo/groupTuxedo?data=' + lo
												// });
											}
										}
									})
									.catch();
							} else if (res.cancel) {
								this.loading = false;
								this.payType = 2;
								uni.setNavigationBarTitle({
									title: '支付失败'
								});
							}
						}
					});
				} else {
					this.wePay();
				}
			}, 500),
			wechatPay(item) {
				this.loading = true;
				// let faildata = JSON.stringify(item)
				const {
					opsData,
					params
				} = this;
				var lo = JSON.stringify(opsData);
				const versionFlag = uni.getSystemInfoSync().version;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: item.timeStamp,
					nonceStr: item.nonceStr,
					package: item.package,
					signType: item.signType,
					paySign: item.paySign,
					success: res => {
						this.loading = false;
						console.log(res);
						this.payType = 1;
						// uni.reLaunch({
						// 	url:"/pageD/pay-Success/pay-Success?payType=1"
						// })
						if (params.type != 3) {
							uni.setNavigationBarTitle({
								title: '支付成功'
							});
						} else {
							if (!versionFlag) {
								window.location.href =
									'https://shop.jyecloud.cn/h5/pageB/groupTuxedo/groupTuxedo?id=' + opsData
									.user_assemble_id + '&type=' + opsData.type;
							} else {
								uni.reLaunch({
									url: '/pageB/groupTuxedo/groupTuxedo?id=' + opsData
										.user_assemble_id + '&type=' + opsData.type
								});
							}
							// uni.reLaunch({
							// 	url: '/pageB/groupTuxedo/groupTuxedo?data=' + lo
							// });
						}
					},
					fail: err => {
						this.loading = false;
						this.payType = 2;
						uni.setNavigationBarTitle({
							title: '支付失败'
						});
						// uni.reLaunch({
						// 	url:"/pageD/pay-Success/pay-Success?payType=2&data="+faildata
						// })
						// console.log('fail:' + JSON.stringify(err));
					}
				});
			},
			h5pay(item) {
				const {
					opsData,
					params
				} = this;
				var lo = JSON.stringify(opsData);
				const versionFlag = uni.getSystemInfoSync().version;
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						appId: item.appId, //公众号名称，由商户传入
						timeStamp: item.timeStamp, //时间戳，自1970年以来的秒数
						nonceStr: item.nonceStr, //随机串
						package: item.package,
						signType: item.signType, //微信签名方式：
						paySign: item.paySign //微信签名
					},
					res => {
						if (res.err_msg == 'get_brand_wcpay_request:ok') {
							// uni.reLaunch({
							// 	url:"/pageD/pay-Success/pay-Success?payType=1"
							// })
							this.payType = 1;
							if (params.type != 3) {
								uni.setNavigationBarTitle({
									title: '支付成功'
								});
							} else {
								if (!versionFlag) {
									window.location.href =
										'https://shop.jyecloud.cn/h5/pageB/groupTuxedo/groupTuxedo?id=' + opsData
										.user_assemble_id + '&type=' + opsData.type
								} else {
									uni.reLaunch({
										url: '/pageB/groupTuxedo/groupTuxedo?id=' + opsData.user_assemble_id +
											'&type=' + opsData.type
									});
								}
								// uni.reLaunch({
								// 	url: '/pageB/groupTuxedo/groupTuxedo?data=' + lo
								// });
							}

							//var url = window.location.host;
							//location.href = "https://" + url + "/h5/pages/order/index";
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						} else {
							this.payType = 2;
							// uni.reLaunch({
							// 	url:"/pageD/pay-Success/pay-Success?payType=2"
							// })
							uni.setNavigationBarTitle({
								title: '支付失败'
							});
						}
					}
				);
			}
		}
	};
</script>
<style>
	page {
		background-color: #ffffff;
	}
</style>
<style lang="scss">
	$succeee: #09bb07;
	$lose: #ff2842;

	.pay {
		&-view {
			padding: 200upx 0;

			image {
				width: 145upx;
				height: 154upx;
				display: block;
				margin: 0 auto;
			}

			text {
				font-size: 36upx;
				color: #333333;
				display: block;
				text-align: center;
				margin: 30upx 0;
			}

			p {
				font-size: 30upx;
				color: #666666;
				text-align: center;
			}
		}

		&-btn {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30upx;

			view {
				width: 48%;
				height: 80upx;
				text-align: center;
				line-height: 80upx;
				font-size: 32upx;
				border-radius: 40upx;

				&:first-child {
					border: 2upx solid #666666;
					color: #666666;
				}

				&:last-child {
					color: #ffffff;
				}
			}

			&-succ {
				background-color: $succeee;
			}

			&-lose {
				background-color: $lose;
			}
		}
	}
</style>